#ifndef __GLF_FILESTREAMMGR_H_INCLUDED__
#define __GLF_FILESTREAMMGR_H_INCLUDED__

#if GLF_VIRTUAL_FILESTREAM

#include <glf/io/fileStream.h>
#include <glf/core/genericRecursiveMutex.h>
#include <glf/core/spinLock.h>
#include <vector>

namespace glf {

class FileStreamMgr
{
	friend class FileStream;

public:

	FileStreamMgr();
	~FileStreamMgr();

	void FileOutAllFiles();

private:

	bool Open(FileStream* fileStream, const char* filename, ios::openflag flag);
	void Close(FileStream* fileStream);

	void Ensure(FileStream* fileStream);

	void Insert(FileStream* fileStream);

	uint64 IncrCounter();

	struct CounterComparator
	{
		bool operator()(const FileStream* fs1, const FileStream* fs2) const
		{
			return fs1->GetCounter() > fs2->GetCounter();
		}
	};

	static const size_t MAX_FILESTREAM = GLF_MAX_FILESTREAM;

	std::vector<FileStream*> mActiveList;
	std::vector<FileStream*> mInactiveList;
	GenericRecursiveMutex<SpinLock> mMutex;

	typedef GenericRecursiveMutex<SpinLock>::LockScope LockScope;

	uint64 mCounter;
};

} // end namespace glf

#endif	// GLF_VIRTUAL_FILESTREAM
#endif	// __GLF_FILESTREAMMGR_H_INCLUDED__
